package models

import (
	"gorm.io/gorm"
	"time"
	"user_srv/basic/config"
)

type Video struct {
	Id        uint           `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	UserId    uint           `gorm:"column:user_id;type:bigint(20);comment:发布人;not null;" json:"user_id"`                                                // 发布人
	Title     string         `gorm:"column:title;type:varchar(255);comment:标题;not null;" json:"title"`                                                    // 标题
	Describe  string         `gorm:"column:describe;type:varchar(255);comment:描述;not null;" json:"describe"`                                              // 描述
	VideoUrl  string         `gorm:"column:video_url;type:varchar(255);comment:封面;not null;" json:"video_url"`                                            // 封面
	LikeCount uint           `gorm:"column:like_count;type:int(20);comment:点赞数;not null;" json:"like_count"`                                             // 点赞数
	PlayCount uint           `gorm:"column:play_count;type:int(20);comment:播放量;not null;" json:"play_count"`                                             // 播放量
	VideoType string         `gorm:"column:video_type;type:varchar(255);comment:视频类型;not null;" json:"video_type"`                                      // 视频类型
	Status    uint           `gorm:"column:status;type:int(10) UNSIGNED;comment:视频状态：1:待审核，2:审核成功，3:审核失败;not null;default:1;" json:"status"` // 视频状态：1:待审核，2:审核成功，3:审核失败
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (v *Video) TableName() string {
	return "video"
}
func (v *Video) AddVideo(db *gorm.DB) error {
	return db.Create(&v).Error
}
func (v *Video) ListVideo(page, pageSize int) (ListVideos []*Video, err error) {
	err = config.DB.Where("video_type like ?", "%"+v.VideoType+"%").Scopes(Paginate(page, pageSize)).Find(&ListVideos).Error
	return
}
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
func (v *Video) DetailVideo(db *gorm.DB) error {
	return db.Where("id = ?", v.Id).Limit(1).Find(&v).Error
}

func (v *Video) UpdatePlayCount(db *gorm.DB) error {
	return db.Model(&Video{}).Where("id = ?", v.Id).Update("play_count", gorm.Expr("play_count+1")).Error
}
func (v *Video) UpdatePLikeCount(db *gorm.DB) error {
	return db.Model(&Video{}).Where("id = ?", v.Id).Update("like_count", gorm.Expr("like_count+1")).Error
}
func (v *Video) DeleteLikeCount(db *gorm.DB) error {
	return db.Model(&Video{}).Where("id = ?", v.Id).Update("like_count", gorm.Expr("like_count-1")).Error
}
func (v *Video) GetLikeCount() error {
	return config.DB.Where("id = ?", v.Id).Limit(1).Find(&v).Error
}
